using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using LiveStock;
using LiveStock.dataAccess;


namespace LiveStock
{
    public partial class RecommendOption : FadeForm
    {
        private CurrentUser _curUser;
        
        public RecommendOption(CurrentUser curUser)
        {
            InitializeComponent();
            _curUser = curUser;
        }

        ListViewColumnSorter optionSort;
        private void RecommendOption_Load(object sender, EventArgs e)
        {
            fillListView();
            optionSort = new ListViewColumnSorter();
            RecomOp_lvOptions.ListViewItemSorter = optionSort;
            Users.UsersDataTable udt = UsersData.GetUserByID(_curUser.UserID);
            RecomOp_txtRecPer.Text =  udt[0].SavedRecommendationLimit.ToString();
        }

        private void fillListView()
        {
            Keywords.KeywordsDataTable kdt = KeywordsData.KeywordsGetAll(_curUser.UserID);
            for (int i = 0; i < kdt.Rows.Count; i++)
            {
                Word temp = new Word(kdt[i].KeyWordID, kdt[i].KeyWord, kdt[i].Weight);
                if (temp.Weight == 0 && kdt.Rows[i].ItemArray[3].Equals(1))
                {
                    //Do nothing...
                }
                else
                {
                    string[] tempStr = new string[]
                    {
                    temp.KeyWordID.ToString(),
                    temp.KeyWord,
                    temp.Weight.ToString()
                    };
                    ListViewItem item = new ListViewItem(tempStr);
                    RecomOp_lvOptions.Items.Add(item);
                }
            }
            alternateListColors(RecomOp_lvOptions);
        }

        private void alternateListColors(ListView lv)
        {
            if (lv.Items.Count > 0)
            {
                for (int i = 0; i < lv.Items.Count; i++)
                {
                    if (i % 2 != 0)
                    {
                        lv.Items[i].BackColor = Color.White;
                        lv.Items[i].ForeColor = Color.Black;
                    }
                    else
                        lv.Items[i].BackColor = Color.LightSteelBlue;
                    lv.Items[i].ForeColor = Color.Black;
                }
            }
        }

        private void RecomOp_btnAdd_Click(object sender, EventArgs e)
        {
            Weight newWeight = new Weight(_curUser);
            newWeight.ShowDialog();
            RecomOp_lvOptions.Items.Clear();
            fillListView();
        }

        private void RecomOp_btnDelete_Click(object sender, EventArgs e)
        {
           
            Keywords.KeywordsDataTable kr = KeywordsData.KeywordsByKewWordID(Int32.Parse(RecomOp_lvOptions.Items[RecomOp_lvOptions.SelectedItems[0].Index].SubItems[0].Text));
            Word newWord = new Word();
            newWord.DefaultWeight = Int32.Parse(kr.Rows[0].ItemArray[4].ToString());
            newWord.KeyWord = kr.Rows[0].ItemArray[1].ToString();
            newWord.KeyWordID = Int32.Parse(kr.Rows[0].ItemArray[0].ToString());
            newWord.UserID = Int32.Parse(kr.Rows[0].ItemArray[3].ToString());
            newWord.Weight = 0;

            DialogResult r = MessageBox.Show("Delete the keyword \"" + newWord.KeyWord + "\"?", "Delete Keyword?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (r == DialogResult.Yes)
            {
                KeywordsData.KeywordDeleteByID(newWord.KeyWordID);

                RecomOp_lvOptions.Items.Clear();
                fillListView();
            } 
        }

        private void RecomOp_btnModify_Click(object sender, EventArgs e)
        {            
            if (RecomOp_lvOptions.SelectedItems.Count > 0)
            {
                int wordID = Int32.Parse(RecomOp_lvOptions.Items[RecomOp_lvOptions.SelectedItems[0].Index].SubItems[0].Text);
                string tempWord = RecomOp_lvOptions.Items[RecomOp_lvOptions.SelectedItems[0].Index].SubItems[1].Text;
                int tempWeight = Int32.Parse(RecomOp_lvOptions.Items[RecomOp_lvOptions.SelectedItems[0].Index].SubItems[2].Text);
                Word modWord = new Word(wordID, tempWord, tempWeight);
                Weight newWeight = new Weight(modWord, _curUser);
                newWeight.ShowDialog();
                int selectedIndex = -1;
                if(RecomOp_lvOptions.SelectedItems.Count > 0)
                {
                    selectedIndex = RecomOp_lvOptions.SelectedIndices[0];
                }
                RecomOp_lvOptions.Items.Clear();
                fillListView();
                if (selectedIndex != -1)
                {
                    RecomOp_lvOptions.Items[selectedIndex].Selected = true;
                    RecomOp_lvOptions.Select();
                }
            }
            else
            {
                MessageBox.Show("You must select an item to modify.");
            }
        }

        private void RecomOp_lvOptions_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            // Determine if clicked column is already the column that is being sorted.
            if (e.Column == optionSort.SortColumn)
            {
                // Reverse the current sort direction for this column.
                if (optionSort.Order == SortOrder.Ascending)
                {
                    optionSort.Order = SortOrder.Descending;
                }
                else
                {
                    optionSort.Order = SortOrder.Ascending;
                }
            }
            else
            {
                // Set the column number that is to be sorted; default to ascending.
                optionSort.SortColumn = e.Column;
                optionSort.Order = SortOrder.Descending;
            }

            // Perform the sort with these new sort options.
            this.RecomOp_lvOptions.Sort();
            alternateListColors(RecomOp_lvOptions);
        }

        private void RecomOp_btnDefaults_Click(object sender, EventArgs e)
        {
            DialogResult r = MessageBox.Show("Restoring will bring back LiveStock DEFAULT KEYWORDS,\nDELETING ANY USER DEFINED KEYWORDS\nAre you sure you wish to procede?", "Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
            if (r == DialogResult.Yes)
            {
                KeywordsData.restoreDefaultKeywords(_curUser.UserID);
                RecomOp_lvOptions.Items.Clear();
                fillListView();
            }
        }

        private void RecomOp_btnChange_Click(object sender, EventArgs e)
        {
            
            UsersData.setSavedRecommendationLimit(_curUser.UserID, Int32.Parse(RecomOp_txtRecPer.Text));
            Users.UsersDataTable udt = UsersData.GetUserByID(_curUser.UserID);
            RecomOp_txtRecPer.Text = udt[0].SavedRecommendationLimit.ToString();
            MessageBox.Show("Saved Recommendation Limit Successfully Changed", "Changed.", MessageBoxButtons.OK, MessageBoxIcon.Information); 
        }

        private void RecomOp_lvOptions_KeyDown(object sender, KeyEventArgs e)
        {
            for (int i = 0; i < RecomOp_lvOptions.Items.Count;i++)
            {

                if (RecomOp_lvOptions.Items[i].SubItems[1].Text.StartsWith(e.KeyData.ToString()) || RecomOp_lvOptions.Items[i].SubItems[1].Text.StartsWith(e.KeyData.ToString().ToLower()))
                {
                    RecomOp_lvOptions.Items[i].Selected = true;
                    RecomOp_lvOptions.Select();
                    RecomOp_lvOptions.Items[i].EnsureVisible();
                    break;
                }
            }
        }

        private void RecomOp_lvOptions_Leave(object sender, EventArgs e)
        {
            if (RecomOp_lvOptions.SelectedItems.Count > 0)
            {
                alternateListColors(RecomOp_lvOptions);
                RecomOp_lvOptions.SelectedItems[0].BackColor = System.Drawing.Color.RoyalBlue;
                RecomOp_lvOptions.SelectedItems[0].ForeColor = System.Drawing.Color.White;
            }
        }
    }
}